home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / readers / skim-0.8 / skim-0 / skim-0.8.4 / MemAlloc.c < prev    next >
C/C++ Source or Header  |  1996-02-18  |  4KB  |  157 lines

  1. /*
  2.  * NAME
  3.  *   MemAlloc.c
  4.  * DESCRIPTION
  5.  *   Routines for memory allocation with simple exception handling.
  6.  * COPYRIGHT
  7.  *   MemAlloc - Memory allocation routines with builtin error handling.
  8.  *   Copyright (C) 1996  Rene W.J. Pijlman
  9.  *
  10.  *   This program is free software; you can redistribute it and/or modify
  11.  *   it under the terms of the GNU General Public License as published by
  12.  *   the Free Software Foundation; either version 2 of the License, or
  13.  *   (at your option) any later version.
  14.  *
  15.  *   This program is distributed in the hope that it will be useful,
  16.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  *   GNU General Public License for more details.
  19.  *
  20.  *   You should have received a copy of the GNU General Public License
  21.  *   along with this program; if not, write to the Free Software
  22.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  * VERSION
  24.  *   $Header: /home/rene/sys/CVS_MasterSourceRepository/skim/MemAlloc.c,v 1.5 1996/02/16 23:10:48 rene Exp $
  25.  *   Distributed with Skim version 0.8.4.
  26.  */
  27.  
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <assert.h>
  32.  
  33. #include "MemAlloc.h"
  34.  
  35. #define CLASS_NAME "Mem"
  36. #include "CheckForDebris.h"
  37.  
  38. static void Terminate( size_t Size )
  39. {
  40.     fprintf( stderr,
  41.     "Cannot allocate %u bytes. Terminating. Add RAM or increase swap space.\n",
  42.              Size );
  43. #ifdef NDEBUG
  44.     exit( EXIT_FAILURE );
  45. #else
  46.     /* Force a core dump for debugging. */
  47.     abort();
  48. #endif
  49. }
  50.  
  51.  
  52. /*
  53.  * DESCRIPTION
  54.  *     Return a pointer to uninitialized memory of Size bytes. The
  55.  *     pointer is properly aligned for any datatype.
  56.  * REQUIRE
  57.  * ENSURE
  58.  *     ( Size == 0 && Result == NULL ) || Result != NULL;
  59.  */
  60. void * MemAlloc( size_t Size )
  61. {
  62.     void * Result = NULL;
  63.  
  64.     if ( Size > 0 )
  65.     {
  66.     Result = malloc( Size );
  67.     IncrementObjectCount();
  68.  
  69.     if ( Result == NULL )
  70.     {
  71.         Terminate( Size );
  72.     }
  73.     }
  74.  
  75.     assert( ( Size == 0 && Result == NULL ) || Result != NULL );
  76.  
  77.     return Result;
  78. }
  79.  
  80.  
  81. /*
  82.  * DESCRIPTION
  83.  *     If OldAdress is non-NULL, the size of the object pointed to by
  84.  *     OldAddress is changed to NewSize. Otherwise, NewSize bytes are
  85.  *     allocated. A pointer to the properly aligned new space is returned.
  86.  *
  87.  *     The contents, if any, will be unchanged up to the minimum of the old 
  88.  *     and the new sizes. If the new space is larger, the new space is 
  89.  *     unitialized.
  90.  * REQUIRE
  91.  * ENSURE
  92.  *     ( NewSize == 0 && Result == NULL ) || Result != NULL;
  93.  */
  94. void * MemRealloc( void * OldAddress, size_t NewSize )
  95. {
  96.     void * Result;
  97.  
  98.     if ( NewSize > 0 )
  99.     {
  100.     if ( OldAddress != NULL )
  101.     {
  102.         Result = realloc( OldAddress, NewSize );
  103.     }
  104.     else
  105.     {
  106.         Result = malloc( NewSize );
  107.         IncrementObjectCount();
  108.     }
  109.  
  110.     if ( Result == NULL )
  111.     {
  112.         Terminate( NewSize );
  113.     }
  114.     }
  115.     else
  116.     {
  117.         if ( OldAddress != NULL )
  118.         {
  119.             free( OldAddress );
  120.         DecrementObjectCount();
  121.         }
  122.  
  123.         Result = NULL;
  124.     }
  125.  
  126.     assert( ( NewSize == 0 && Result == NULL ) || Result != NULL );
  127.  
  128.     return Result;
  129. }
  130.  
  131.  
  132. /*
  133.  * DESCRIPTION
  134.  *     If Address is non-NULL, deallocates the space pointed to by Address. 
  135.  * REQUIRE
  136.  * ENSURE
  137.  */
  138. void MemFree( void * Address )
  139. {
  140.     if ( Address != NULL )
  141.     {
  142.     free( Address );
  143.  
  144.     DecrementObjectCount();
  145.     }
  146. }
  147.  
  148.  
  149. char * MemNewString( const char * Original )
  150. {
  151.     char * New = (char *)MemAlloc( strlen(Original) + 1 );
  152.  
  153.     strcpy( New, Original );
  154.  
  155.     return New;
  156. }
  157.